package com.twu.biblioteca;

import com.twu.biblioteca.controllers.BookController;
import com.twu.biblioteca.controllers.OptionController;
import com.twu.biblioteca.controllers.UserController;
import com.twu.biblioteca.models.*;
import com.twu.biblioteca.providers.BookProvider;
import com.twu.biblioteca.providers.MovieProvider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BibliotecaApp {

    private static String WELCOME_MESSAGE = "Welcome to the Bangalore Public Library";
    private static String TALK_TO_LIBRARIAN = "Please talk to a Librarian. Thank you.";
    private static String INSERT_BOOK_CODE = "Insert the code of the book you want to reserve:";
    private static String SUCCESSFUL_RESERVATION = "Thank You! Enjoy the book.";
    private static String FAILED_RESERVATION = "Sorry we don't have that book yet.";
    private static String SHOW_BOOKS_INTRO = "List of books in the library:";
    private static String ASK_FOR_OPTION = "Insert the corresponding no of the option you want to choose:";
    private static String WRONG_OPTION = "Select a valid option!!";
    private static String NOT_LOGGED_IN_RESERVATION = "Sorry you are not logged in and you cannot reserve a book! Please log in.";
    private static String LOGGED_IN_SUCCESSFULLY = "You have logged in successfully";
    private static String WRONG_CREDENTIALS = "Your credentials are wrong!";
    private static String EXIT_MESSAGE = "Thank you for using the Bangalore Public Library app";
    private static String MOVIE_LIST_TITLE = "Movie - Year - Director - Rating";

    private static BufferedReader bufferedReader;
    private static OptionController optionController;
    private static BookController bookController;
    private static MovieProvider movieProvider;
    private static UserController userController;

    private static User loggedUser;
    private static boolean shouldExit = false;

    public static void main(String[] args) {
        setup();
        run();
    }

    public static void setup(){
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        optionController = new OptionController();
        bookController = new BookController(new BookProvider());
        movieProvider = new MovieProvider();
        userController = new UserController();

        loggedUser = null;
        shouldExit = false;
    }

    public static void run(){
        showWelcomeMessage();
        showOptionList();

        String option;
        while (!shouldExit){

        option = getOptionFromUser();

        if (option.equals("1")) showBooksInTheLibrary();
        if (option.equals("2")) {
            if (loggedUser!=null) reserveBook();
            else System.out.println(NOT_LOGGED_IN_RESERVATION);
        }
        if (option.equals("3")) checkMembershipDetails();
        if (option.equals("4")) showMovieList();
        if (option.equals("5")) loginUser();
        if (option.equals("6")) exitApp();

        }

    }

    public static void loginUser() {
        LoginCredentials credentials = getCredentials();
        boolean ok = userController.validate(credentials);
        if (ok){
            loggedUser = userController.getUserWithCredentials(credentials);
            System.out.println(LOGGED_IN_SUCCESSFULLY);
        }else{
            System.out.println(WRONG_CREDENTIALS);
        }
    }

    private static void exitApp() {
        System.out.println(EXIT_MESSAGE);
        shouldExit = true;
    }

    private static void showMovieList() {
        System.out.println(MOVIE_LIST_TITLE);
        Movie[] movies = movieProvider.getAllMovies();
        for (int i=0;i<movies.length;i++){
            System.out.println(movies[i].toString());
        }
    }

    private static void showWelcomeMessage(){
        System.out.println(WELCOME_MESSAGE);
    }


    private static void showOptionList(){
        optionController.showOptions();
    }

    private static void checkMembershipDetails(){
        if (loggedUser==null) System.out.println(TALK_TO_LIBRARIAN);
        else {
            System.out.println("Hello "+loggedUser.name+", your details are below:");
            System.out.println(" - Email: "+loggedUser.email);
            System.out.println(" - Phone: "+loggedUser.phone);
        }
    }

    private static void reserveBook(){
        System.out.println(INSERT_BOOK_CODE);
        String code = getBookCodeFromUser();

        boolean wasBookReserved = bookController.reserveBookWithCode(code);

        if (wasBookReserved) System.out.println(SUCCESSFUL_RESERVATION);
        else System.out.println(FAILED_RESERVATION);
    }


    private static void showBooksInTheLibrary(){
        Book[] books = bookController.getAllBooks();

        System.out.println(SHOW_BOOKS_INTRO);

        for (int i=0;i<books.length;i++){
            System.out.println(books[i]);
        }
    }

    public static void askToSelectAnOption() {
        System.out.println(ASK_FOR_OPTION);
    }

    public static String getOptionFromUser() {
        askToSelectAnOption();
        String s = null;
        try{
            s = bufferedReader.readLine();
            verifyOption(s);
        }catch(IOException e){
            e.printStackTrace();
        }
        return s;
    }


    private static void verifyOption(String optionShortcut){
        boolean ok = optionController.verifyShortcut(optionShortcut);
        if (!ok) System.out.println(WRONG_OPTION);
    }

    public static User getLoggedUser() {
        return loggedUser;
    }

    public static String getBookCodeFromUser() {
        String s = null;
        try{
            s = bufferedReader.readLine();
        }catch(IOException e){
            e.printStackTrace();
        }
        return s;
    }

    public static void setBookController(BookController _bookController){
        bookController = _bookController;
    }

    public static void setOptionController(OptionController _optionController){
        optionController = _optionController;
    }

    public static void setBufferedReader(BufferedReader _bufferedReader) {
        bufferedReader = _bufferedReader;
    }

    public static void cleanUp(){
        bufferedReader = null;
        optionController = null;
        bookController = null;
    }

    public static LoginCredentials getCredentials() {
        String user = null;
        String pass = null;
        try{
            System.out.println("Please enter your username:");
            user = bufferedReader.readLine();
            System.out.println("Please enter your password:");
            pass = bufferedReader.readLine();

        }catch(IOException e){
            e.printStackTrace();
        }

        return new LoginCredentials(user, pass);
    }


}
